//
//  DropDownMenu.m
//  微博
//
//  Created by 杨佩璋 on 15/6/27.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "DropDownMenu.h"

@interface DropDownMenu ()
@property (nonatomic,retain)UIImageView *containerView;
@end

@implementation DropDownMenu

- (UIImageView *)containerView
{
    if (!_containerView) {
        UIImageView *dropDownMenu = [[UIImageView alloc] init];
        dropDownMenu.userInteractionEnabled = YES;
        dropDownMenu.image = [UIImage imageNamed:@"popover_background"];
        [self addSubview:dropDownMenu];
        self.containerView = dropDownMenu;
        [dropDownMenu release];
    }
    return _containerView;
}

+(instancetype)menu
{
    return [[[self alloc] init] autorelease];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

-(void)showFrom:(UIView *)from{
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];
    self.frame = window.bounds;
    [window addSubview:self];
    CGRect newRect = [from convertRect:from.bounds toView:nil];
    self.containerView.y = CGRectGetMaxY(newRect);
    self.containerView.centerX = CGRectGetMidX(newRect);
    
    if ([self.delegate respondsToSelector:@selector(showUpArrow)]) {
        [self.delegate showUpArrow];
    }
}

-(void)dismiss
{
    [self removeFromSuperview];
    if ([self.delegate respondsToSelector:@selector(showDownArrow)]) {
        [self.delegate showDownArrow];
    }
}

-(void)setContent:(UIView *)content
{
    _content = content;
    _content.x = 10;
    _content.y = 15;
    self.containerView.height = CGRectGetMaxY(_content.frame) + 11;
    self.containerView.width = CGRectGetMaxX(_content.frame) + 10;
    [self.containerView addSubview:self.content];
}

- (void)setContentController:(UIViewController *)contentController
{
    _contentController = contentController;
    self.content = contentController.view;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self dismiss];
}
@end
